package com.kusiri.kframework.plugin
{

 	import flash.display.Loader;
 	import flash.events.Event;
 	import flash.events.EventDispatcher;
 	import flash.net.URLRequest;
 	import flash.system.ApplicationDomain;
 	import flash.system.LoaderContext;
 	
 	import mx.collections.ArrayCollection;
 	
 	public class KfPluginLoader extends EventDispatcher 
 	{

        private var incomplete : int = 0;
        private var callback : Function;
        
        public function load(plugins:ArrayCollection, callback:Function) : void
        {
        	incomplete = plugins.length;
        	this.callback = callback;
        	
        	if (incomplete == 0) 
        	{
        		callback();
        		return;	
        	}
        	
        	for each ( var pluginUrl:String in plugins)
        	{
	            var ldr:Loader = new Loader();
	            var req:URLRequest = new URLRequest(pluginUrl);
	            var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
	            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
	            ldr.load(req, ldrContext);            		
        	}

        }
        
        private function completeHandler(event:Event) : void
        {
            incomplete--;
            if (incomplete==0) callback();
        }

  	}
}